using LitJson;
using UnityEngine;
using UnityEngine.UI;
namespace CGF
{
    public partial class PanelLeaseDevicesDetail : UIComponentBase, IAwake<UILayer, bool>, IDestroy
    {
        private JsonData data;

        public static void Open(JsonData data)
        {
            UIManagerComponent.instance.OpenUI<PanelLeaseDevicesDetail>(UILayer.Normal, RootComponent.instance, false).SetInfo(data);
        }

        public static void Close()
        {
            UIManagerComponent.instance.CloseUI<PanelLeaseDevicesDetail>();
        }

        public async void Awake(UILayer layer, bool isFloat)
        {
            await SpawnPrefab("Prefabs/GameUI/PanelLeaseDevicesDetail", layer, isFloat);
            BindProperties(transform);

            BtnBack.SetClick(Close);

            InputNum.SetInputAction(input =>
            {
                if (int.TryParse(InputNum.text, out int value))
                {
                    data["num"] = Mathf.Clamp(value, 1, Config.LeaseMaxOneTimes);
                    InputNum.text = data["num"].ToString();
                    TextTotalPrice.text = $"{I18N.Currency} {StringHelper.Format2Decimal((int)data["price"] * 0.01f * (int)data["num"])}";
                }
            }, true);

            BtnSub.SetClick(() =>
            {
                if (InputNum.text == "")
                {
                    InputNum.text = "1";
                }
                if (int.Parse(InputNum.text) > 1)
                {
                    InputNum.text = (int.Parse(InputNum.text) - 1).ToString();
                }
            });

            BtnAdd.SetClick(() =>
            {
                if (InputNum.text == "")
                {
                    InputNum.text = "1";
                }
                InputNum.text = Mathf.Min(Config.LeaseMaxOneTimes, int.Parse(InputNum.text) + 1).ToString();
            });

            BtnLease.SetClick(() =>
            {
                int myLevel = (int)UserInfo.Info["level"];
                if (myLevel < (int)data["buy_level"])
                {
                    Dialog.Alert(RootComponent.instance.GetComponent<PanelLeaseDevices>().I18N_level_not_enough);
                    return;
                }
                PanelLeasePay.Open(data, null);
            });

            if (data != null)
            {
                SetInfo(data);
            }
        }

        private void SetInfo(JsonData data)
        {
            //Debug.Log($">>>>>>>>>>>>>>> {data.ToJson()}");
            this.data = data;
            if (TextTitle)
            {
                int type = Mathf.Max(1, (int)data["type"]);
                Color bottomColor = Color.black;
                Color btnColor = Color.black;
                switch (type)
                {
                    case 1:
                        //体验版
                        bottomColor = ColorHelper.Color("3C2D58");
                        btnColor = ColorHelper.Color("A362F4");
                        break;
                    case 2:
                        //专业版
                        bottomColor = ColorHelper.Color("3E376A");
                        btnColor = ColorHelper.Color("555195");
                        break;
                    case 3:
                        //旗舰版
                        bottomColor = ColorHelper.Color("5E4728");
                        btnColor = ColorHelper.Color("A37B4C");
                        break;
                }
                Bg.sprite = Bg.gameObject.Get<Sprite>(type - 1);
                HeadBg.sprite = HeadBg.gameObject.Get<Sprite>(type - 1);
                Bottom.color = bottomColor;
                BtnLease.GetComponent<Image>().color = btnColor;
                Device.GetComponent<Device>().SetInfo(type, (int)data["period"], false);
                Income.text = StringHelper.SurportSpace(data["subtitle"].ToString());
                float[] starProgresses = { 0, 0.086f, 0.195f, 0.299f, 0.42f, 0.5f, 0.609f, 0.707f, 0.816f, 0.919f, 1 };
                int star = string.IsNullOrEmpty(data["input6"].ToString()) ? 0 : int.Parse(data["input6"].ToString());
                StartsFor.fillAmount = Mathf.Clamp(starProgresses[star], 0, 10);
                TextTitle.text = Utils.GetI18N(data["name"].ToString());
                TextPrice.text = $"{I18N.Currency} {StringHelper.Format2Decimal((int)data["price"] * 0.01f)}";
                TextCountry.text = $"{I18N_country}：{Utils.GetI18N(data["input2"].ToString())}";
                TextModel.text = $"{I18N_model}：{Utils.GetI18N(data["input4"].ToString())}";
                TextSpeed.text = $"{I18N_speed}：{Utils.GetI18N(data["input5"].ToString())}";
                TextVersion.text = $"{I18N_version}：{Utils.GetI18N(data["input3"].ToString())}";
                TextContent.text = Utils.GetI18N(data["desc"].ToString());
                InputNum.text = data["num"].ToString();
                TextTotalPrice.text = $"{I18N.Currency} {StringHelper.Format2Decimal((int)data["price"] * 0.01f * (int)data["num"])}";
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }

    }
}
